'''
调试子界面类， 使用sub_debug.ui生成的布局
手动触发
'''
import enum
import sys
import os
import time
import traceback
from functools import partial

from PyQt5 import QtWidgets

from PyQt5.QtWidgets import QApplication, QHeaderView, QWidget, QMessageBox

from ui.sub_debug import Ui_Form  # 界面控件， QtDesigner生成


from log import logger


from plc_S7.HuoniPlcScanClient import Config as plc_config

class SubDebugWidget(QWidget, Ui_Form):

    def __init__(self, parent=None, main_ui_handle=None):
        super(SubDebugWidget, self).__init__(parent)

        self.setupUi(self)
        self.main_ui_handle = main_ui_handle

        # 额外样式
        self.setStyleSheet("""
                QPushButton {
                    min-width: 80px;
                    padding: 5px;
                    border-radius: 3px;
                    color: white;
                    background-color: dodgerblue;
                }
                QPushButton:hover {
                    background-color: darkblue;
                }
                
                /* 定义 QGroupBox 样式 */
                QGroupBox {
                    /*font-size: 14px;*/
                    font-weight: bold;
                    border: 1px solid #ccc;
                    border-radius: 8px; /* 使用像素值代替分数 */
                    padding: 1em;
                    margin-top: 1em;
                    background-color: #f5f5f5;
                }
                
                /* 设置标题样式 */
                QGroupBox::title {
                    subcontrol-origin: margin;
                    /*subcontrol-position: top center;*/
                    padding: 0 1em;
                    color: #333;
                    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                                    stop: 0 #f5f5f5, stop: 1 #e5e5e5);
                    border-bottom: 1px solid #ccc;
                    border-top-left-radius: 8px;
                    border-top-right-radius: 8px;
                }
                
                /* 输入框样式 */
                QLineEdit {
                    border: 1px solid #ccc; /* 边框颜色 */
                    border-radius: 4px; /* 边框圆角 */
                    background-color: #fff; /* 背景颜色 */
                }
                /* 输入框焦点样式 */
                QLineEdit:focus {
                    border-color: #80bfff; /* 焦点边框颜色 */
                }
            """)
        self.pushButton_7.clicked.connect(partial(self.munal_trigger_solt, 'print_trigger')) #
        # self.pushButton_8.clicked.connect(partial(self.munal_trigger_solt, 'upload_trigger'))
        self.pushButton.clicked.connect(partial(self.munal_trigger_solt, 'phone_trigger'))
        self.pushButton_2.clicked.connect(partial(self.munal_trigger_solt, 'bat_trigger'))

        self.pushButton_3.clicked.connect(partial(self.munal_trigger_solt, 'cable_trigger'))
        self.pushButton_4.clicked.connect(partial(self.munal_trigger_solt, 'strap_trigger'))
        self.pushButton_5.clicked.connect(partial(self.munal_trigger_solt, 'instru_trigger'))
        self.pushButton_6.clicked.connect(partial(self.munal_trigger_solt, 'license_trigger'))

        self.btnName_plcAds_dict = { # 兼容plc_config格式
            'print_trigger': plc_config.MainTh['is_print_chufa'],
            'upload_trigger': plc_config.MainTh['is_upload_chufa'],
            'phone_trigger': plc_config.PlacePhone['is_check_chufa'],
            'bat_trigger': plc_config.PlaceBat['is_check_chufa'],

            'cable_trigger': plc_config.PlaceCable['is_check_chufa'],
            'strap_trigger': plc_config.PlaceStrap['is_check_chufa'],
            'instru_trigger': plc_config.PlaceInstruction['is_check_chufa'],
            'license_trigger': plc_config.PlaceLicence['is_check_chufa'],

        }

    def munal_trigger_solt(self, btn_name):
        try:

            # if btn_name == 'print_trigger':
            #     sn_str = self.sNLineEdit.text()

            print(f'{btn_name}  {self.btnName_plcAds_dict[btn_name]}')
            value_before = self.main_ui_handle.engine.plc_main_handle.munal_get_chufa(adds_list_or_int= self.btnName_plcAds_dict[btn_name])
            print(f'value_before {value_before} ')
            self.main_ui_handle.engine.plc_main_handle.munal_set_chufa(adds_list_or_int= self.btnName_plcAds_dict[btn_name])

            time.sleep(1)
            value_after = self.main_ui_handle.engine.plc_main_handle.munal_get_chufa(
                adds_list_or_int=self.btnName_plcAds_dict[btn_name])
            print(f'value_after  {value_after} ')
        except:
            print(traceback.format_exc())

    def set_sn_to_plc_slot(self):
        try:
            pass
            # sn_str = self.sNLineEdit.text()
            # print(sn_str)
            # self.main_ui_handle.engine.plc_main_handle.munal_set_str(adds_list_or_int=plc_config.MainTh['serial_number_to_print'], value_str=sn_str)  # todo 解耦
        except:
            print(traceback.format_exc())

    def print_label_slot(self):
        '''
        打印标贴，和plc无交互
        :return:
        '''
        try:
            sn_str = self.sNLineEdit.text()
            is_ok = self.main_ui_handle.engine.manual_print(sn_str)
            if not is_ok:
                # 弹出提示框
                msg_box = QMessageBox()
                msg_box.setWindowTitle('手动打印')
                msg_box.setText('已有上传记录，不能手动打印')
                msg_box.setDefaultButton(QMessageBox.Ok)
                response = msg_box.exec()
                if response == QMessageBox.Ok:
                    print('点击OK')
                    pass
        except:
            print(traceback.format_exc())
    def upload_slot(self):
        '''
        手动上传，和plc无交互
        :return:
        '''
        sn_str = self.sNLineEdit.text()
        self.main_ui_handle.engine.manual_upload(sn_str)

if __name__ == '__main__':
    try:
        app = QtWidgets.QApplication(sys.argv)
        mainWnd = SubDebugWidget()
        # ui = Ui_MainWindow()
        # ui.setupUi(mainWnd)
        mainWnd.show()
        sys.exit(app.exec())
    except:
        print(traceback.format_exc())
        logger.error(traceback.format_exc())